$.mockjaxSettings.responseTime = 10;
$.mockjax({ url: "/true", responseText: true });
$.mockjax({ url: "/false", responseText: false });



module("test plugin method");

test("jQuery.addValidationRule", function(){
	$.addValidationRule("rule name", function(){ return true; }, "default validation message");
	$.addValidationRule("rule name", function(){ return false; }, "default validation message");
	$.addValidationRule("rule name", function(){ return $.ajax(); }, "default validation message");
	
	try{
		$.addValidationRule("rule name");
		ok(false, "Not enough arguments, must throw an exception");
	}catch(e){}
});


test("checkValidity", function(){
	$.addValidationRule("testrule_return_true", function(){ return true; })
	$.addValidationRule("testrule_return_false", function(){ return false; })

	testCheckvalidityReturns("#required", "", false, "invalid");
	testCheckvalidityReturns("#required", "xxx", true, "valid");
	testCheckvalidityReturns("<input data-remote=true />", "", undefined, "pending");

	testCheckvalidityReturns($("<input />").addRule(function(){ return true; }), "", true, "valid");
	testCheckvalidityReturns($("<input />").addRule(function(){ return false; }), "", false, "invalid");

	testCheckvalidityReturns($("<input />").addRule("testrule_return_true"), "", true, "valid");
	testCheckvalidityReturns($("<input />").addRule("testrule_return_false"), "", false, "invalid");
});

test("addRule", function(){
	$("<input />").addRule("required", "validation message");
	$("<input />").addRule("any_rule_name", "validation message");
	$("<input />").addRule(function(){}, "validation message");
});


module("validation event");

test("aftercheckvalidity", function(){
	expect(6);
	stop(6);

	$.addValidationRule("__defined_a_rule__", function(){ return true; });

	testAfterCheckValidityEvent("<input required />", false, "Invalid");
	testAfterCheckValidityEvent("<input required value=x />", true, "valid");
	testAfterCheckValidityEvent("<input remote='/false' />", false, "invalid");
	testAfterCheckValidityEvent("<input remote='/true' />", true, "valid");
	testAfterCheckValidityEvent("<input required remote='/true' />", false, "invalid");
	testAfterCheckValidityEvent("<input required remote='/true' value=x />", true, "valid");
	testAfterCheckValidityEvent($("<input />").addRule());

	try{
		$("<input />").addRule("_undefined_rule_").checkValidity();
		ok(false, "must throw an expection when encounter an undefined-rule");
	}catch(e){}
});

function testCheckvalidityReturns(element, value, expect, testMessage){
	equals($(element).val(value).checkValidity(), expect, testMessage);
}

function testAfterCheckValidityEvent(element, expect, testMessage){
	element = $(element);
	element.bind("aftercheckvalidity", function(event, valid, validationMessage){ equals(valid, expect); start(); });
	element.checkValidity();
}